package interfaces.options;

import fileHandling.language.Lang;
import fileHandling.language.LanguageLoader;
import fileHandling.language.options.OptionValues;
import interfaces.GUISource;
import interfaces.superWidgets.InterfaceLabel;

import java.util.HashMap;

import main.InitGame;
import main.listeners.listenerClasses.LanguageListener;

import org.fenggui.ComboBox;
import org.fenggui.event.ISelectionChangedListener;

public class ComboBoxOptionWidget extends OptionWidget {
	
	private ComboBox comboBox;
	private HashMap<String, String> optionValueMap;
	private String[] values;
	private String lastSelection, high, medium, low;
	private boolean isEnabled = true;
	private InterfaceLabel disabledLabel;
	
	public ComboBoxOptionWidget(String initValue, Lang description, String[] values, 
			String high, String medium, String low) {
		super(description, initValue);
		this.values = values;
		this.high = high;
		this.medium = medium;
		this.low = low;
		
		disabledLabel = new InterfaceLabel("n/a", GUISource.middleFont);

		comboBox = GUISource.createComboBox(GUISource.middleFont);
		setValues(values, initValue);
		
		InitGame.get().addLanguageListener(new LanguageListener() {
			@Override
			public void languageChanged() {
				changeLanguage();
			}
		});
	}
	
	public void setValues(String[] values, String initValue) {
		removeAllWidgets();
		optionValueMap = new HashMap<String, String>();
		
		comboBox.getList().clear();
		for(String key : values) {
			Lang optionValue = null;
			try { optionValue = OptionValues.valueOf(key); }
			catch(Exception e) {}
			
			String valueToAdd = key;
			if(optionValue != null) valueToAdd = LanguageLoader.get(optionValue);
			if(initValue.equals(key)) initValue = valueToAdd;
			optionValueMap.put(key, valueToAdd);
			comboBox.addItem(valueToAdd);
		}
		changeValue(initValue);
		addWidget(comboBox);
	}
	
	@Override
	public String getValue() {
		String optionValue = getSelectedLang();
		String returnValue = optionValue;
		if(optionValue == null) returnValue = comboBox.getSelectedValue();
		if(getContent().contains(disabledLabel)) returnValue = "60";
		return returnValue;
	}
	
	private String getSelectedLang() {
		String selectedValue = comboBox.getSelectedValue();
		for(String key : optionValueMap.keySet()) {
			if(selectedValue.equals(optionValueMap.get(key))) return key;
		}
		return null;
	}
	
	public void addChangeListener(ISelectionChangedListener listener) {
		comboBox.addSelectionChangedListener(listener);
	}
	
	public void setEnabled(boolean enable) {
		if(!enable && isEnabled) {
			lastSelection = getValue();
			removeWidget(comboBox);
			addWidget(disabledLabel);
		} else if(enable && !isEnabled) {
			addWidget(comboBox);
			removeWidget(disabledLabel);
			comboBox.setSelected(lastSelection);
		}
		isEnabled = enable;
	}
	
	public void changeLanguage() {
		String selection = getSelectedLang();
		if(selection != null) setValues(values, selection);
		else setValues(values, comboBox.getSelectedValue());
		isEnabled = !isEnabled;
		lastSelection = selection;
		setEnabled(!isEnabled);
		layout();
	}
	
	private void setDetails(String details) {
		Lang optionValue = null;
		try { optionValue = OptionValues.valueOf(details); }
		catch(Exception e) {}
		
		if(optionValue != null) details = LanguageLoader.get(optionValue);
		changeValue(details);
	}
	
	@Override
	public void setToHigh() { if(high != null) setDetails(high); }
	
	@Override
	public void setToMedium() { if(medium != null) setDetails(medium); }
	
	@Override
	public void setToLow() { if(low != null) setDetails(low); }
	
	@Override
	public void changeValue(String value) { comboBox.setSelected(value); }
}